<template>
  <div class="goods-list">
    <div class="goods-item" v-for="item in goods" :key="item.id" @click="getgoodsinfo(item.id)">
      <img :src="item.img_url" alt />
      <h1 class="title">{{item.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{item.sell_price}}</span>
          <span class="old">￥{{item.market_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{item.stock_quantity}}件</span>
        </p>
      </div>
    </div>
    <mt-button type="danger" size="large" @click="getmoregoods">加载更多</mt-button>
  </div>
</template>

<script>
//按需导入提示组件
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      goods: [],
      pageindex: 1
    };
  },
  created() {
    this.getgoods();
  },
  methods: {
    getgoods() {
      this.$https
        .get(
          "http://www.liulongbin.top:3005/api/getgoods?pageindex=" +
            this.pageindex
        )
        .then(res => {
          //console.log(res.data);
          if (res.data.status === 0) {
            //成功了
            // this.goods = res.data.message;
            //将数组中新数据和数组中老数据拼接起来，组成新的数组。利用concat方法。
            this.goods = this.goods.concat(res.data.message);
          } else {
            Toast("加载商品列表失败");
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
     getmoregoods() {
      this.pageindex++;
      this.getgoods();
    },
    getgoodsinfo(id){
      this.$router.push({name:"goodsinfo",params:{id:id}})//传递的参数名称要和接收的参数名称对应上
      // 否则的话地址显示不正确
    }
  }
};
</script>


<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 7px;
  justify-content: space-between;
  .goods-item {
    width: 49%;
    border: 1px solid #ccc; //浏览器审查元素的时候，元素的边框和内边距都算上。
    box-shadow: 0 0 8px #ccc;
    margin: 4px 0; //盒子框边线到外部的延伸距离。
    padding: 2px; //盒子框边线到元素本身的距离。
    display: flex;
    flex-direction: column; //改变主轴方向为y轴
    justify-content: space-between;
    min-height: 293px;
    img {
      width: 100%; //对比父元素的宽度。
    }
    .title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
        .old {
          font-size: 12px;
          text-decoration: line-through; //加横线
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>